package top.abrsm.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import top.abrsm.entity.AddressEntity;
import top.abrsm.entity.AreaEntity;
import top.abrsm.entity.StudentEntity;
import top.abrsm.service.IAddressService;
import top.abrsm.service.IAreaService;
import top.abrsm.util.ResultBody;
import top.abrsm.util.SnowflakeIdWorker;
import top.abrsm.util.StateEnum;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 * 收货地址表 前端控制器
 * </p>
 * @author WuZH
 * @since 2023-11-18
 */
@Controller
@RequestMapping ("/address")
public class AddressController {

    @Resource
    private IAddressService iAddressService;
    @Resource
    private IAreaService iAreaService;

    @RequestMapping ("getAddressList")
    public String getAddressList (HttpSession session, Model model) {
        StudentEntity studentEntity = (StudentEntity) session.getAttribute ( "studentEntity" );
        AddressEntity addressEntity = new AddressEntity ();
        addressEntity.setStuId ( studentEntity.getId () );
        List<AddressEntity> addressEntities = iAddressService.queryList ( addressEntity );
        model.addAttribute ( "addressEntities", addressEntities );
        return "addressList";
    }

    @RequestMapping ("queryList")
    @ResponseBody
    public ResultBody<AddressEntity> queryList (AddressEntity addressEntity) {
        List<AddressEntity> addressEntities = iAddressService.queryList ( addressEntity );
        return ResultBody.success ( addressEntities );
    }

    @RequestMapping ("querySingle")
    @ResponseBody
    public ResultBody querySingle (AddressEntity addressEntity) {
        addressEntity = iAddressService.querySingle ( addressEntity );
        return ResultBody.success ( addressEntity );
    }

    @RequestMapping ("saveSingle")
    @ResponseBody
    public ResultBody<AddressEntity> saveSingle (AddressEntity addressEntity, HttpSession session) {
        StudentEntity studentEntity = (StudentEntity) session.getAttribute ( "studentEntity" );
        if ( studentEntity == null ) {
            return ResultBody.fail ( StateEnum.SESSION_EXPIRE );
        }
        // 拿到id去转换
        AreaEntity provinceEntity = iAreaService.getBaseMapper ().selectById ( addressEntity.getProvinceId () );
        addressEntity.setProvince ( provinceEntity.getAreaName () );
        AreaEntity cityEntity = iAreaService.getBaseMapper ().selectById ( addressEntity.getCityId () );
        addressEntity.setProvince ( cityEntity.getAreaName () );
        AreaEntity areaEntity = iAreaService.getBaseMapper ().selectById ( addressEntity.getAreaId () );
        addressEntity.setProvince ( areaEntity.getAreaName () );
        addressEntity.setStuId ( studentEntity.getId () );
        addressEntity.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
        addressEntity = iAddressService.saveSingle ( addressEntity );
        return ResultBody.success ( addressEntity );
    }

    /**
     * 修改默认地址
     * @param addressEntity
     * @return
     */
    @RequestMapping ("changeDefault")
    @ResponseBody
    public ResultBody<AddressEntity> changeDefault (AddressEntity addressEntity, HttpSession session) {
        StudentEntity studentEntity = (StudentEntity) session.getAttribute ( "studentEntity" );
        // 当前用户所有地址改为0
        // update address_info set isDefault=0 where stuId=?
        addressEntity.setStuId ( studentEntity.getId () );
        addressEntity = iAddressService.querySingle ( addressEntity );
        iAddressService.updateDefaultAddress ( addressEntity );
        // 修改当前记录为1
        AddressEntity address = new AddressEntity ();
        address.setId ( addressEntity.getId () );
        address.setIsDefault ( 1 );
        address = iAddressService.updateSingle ( address );
        return ResultBody.success ( address );
    }


    @RequestMapping ("updateSingle")
    @ResponseBody
    public ResultBody updateSingle (AddressEntity addressEntity) {
        addressEntity = iAddressService.updateSingle ( addressEntity );
        return ResultBody.success ( addressEntity );
    }

    @RequestMapping ("deleteSingle")
    @ResponseBody
    public ResultBody deleteSingle (AddressEntity addressEntity) {
        addressEntity = iAddressService.deleteSingle ( addressEntity );
        return ResultBody.success ( addressEntity );
    }

}
